package com.example.administrator.dialogs;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button dialog1;
    private Button dialog2;
    private Button dialog3;
    private Button dialog4;
    private Button dialog5;
    private Button dialog6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showSimpleDialog();

        showAlertDialog();

        showItemsDialog();

        showSingleChoiceDialog();

        showMultiDialog();

        showinitDialog();
    }

    //自定义对话框
    private void showinitDialog() {
        dialog6 = findViewById(R.id.dialog6);
        dialog6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建view
                View view = LayoutInflater.from(MainActivity.this)
                        .inflate(R.layout.dialog,null,false);

                final EditText editName = view.findViewById(R.id.editName);
                final EditText editPassword = view.findViewById(R.id.editPassword);

                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("用户注册")
                        .setView(view)
                        .setPositiveButton("注册成功", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String username = editName.getText().toString();
                                int password = Integer.parseInt(editPassword.getText().toString());

                                Toast.makeText(MainActivity.this,
                                        "username:"+ username + "   password:"+ password,
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消注册", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    //多选对话框
    private void showMultiDialog() {
        dialog5 = findViewById(R.id.dialog5);
        dialog5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] hobbies = {"旅行","攀爬","美食","看书","玩游戏","睡觉","游泳"};
                //定义判断数组，默认值为false，若有，则加进该数组
                final Boolean[] isChecked = new Boolean[7];

                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("选择爱好")
                        .setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean b) {
                                //每次选中的值
                                isChecked[which] = b;

                                if(b){
                                    Toast.makeText(MainActivity.this,
                                            "您选中了：" + hobbies[which] + "isChecked : " +isChecked[which],
                                            Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(MainActivity.this,
                                            "您取消了：" + hobbies[which] + "isChecked : " +isChecked[which],
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                StringBuffer buffer = new StringBuffer();

                                for(int i=0; i<isChecked.length; i++){
                                    if(isChecked[i] != null && isChecked[i] == true){
                                        buffer.append(hobbies[i]+"  ");
                                    }

                                   /* Toast.makeText(MainActivity.this,
                                            "您爱好有：" + buffer  + "isChecked : " +isChecked[i],
                                            Toast.LENGTH_SHORT).show();*/
                                }

                                Toast.makeText(MainActivity.this,
                                        "您爱好有：" + buffer,
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    //单选对话框
    private void showSingleChoiceDialog() {
        dialog4 = findViewById(R.id.dialog4);
        dialog4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String sex[] = {"男","女","人妖"};

                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请选择您的性别：")
                        .setSingleChoiceItems(sex, -1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,
                                        "您的性别是" + sex[which],
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    //下拉列表对话框
    private void showItemsDialog() {
        dialog3 = findViewById(R.id.dialog3);
        dialog3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] tourPlaces = {"云南大理","岳阳楼","杭州西湖","韶关丹霞山","广州小蛮腰","长隆乐园",
                "深圳欢乐谷"};

                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请选择想旅游的景点：")
                        .setItems(tourPlaces, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,
                                        "您选择的旅游圣地为：" + tourPlaces[which],
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    //警告对话框
    private void showAlertDialog() {
        dialog2 = findViewById(R.id.dialog2);
        dialog2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("提示：")
                        .setMessage("是否删除")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,
                                        "positive : "+ which,
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,
                                        "negative : "+ which,
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,
                                        "neutal : "+ which,
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    //简单对话框
    private void showSimpleDialog() {
        dialog1 = findViewById(R.id.dialog1);
        dialog1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("今日运势")
                        .setMessage("2018年10月26日，宜动土，宜嫁娶！")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .create()
                        .show();
            }
        });
    }
}
